/**   
 * @Title: ConnectionChangeReceiver.java 
 * @Package: com.esun.network 
 * @Description: TODO(网络状态监测) 
 * @Author: Billy qigs@500wan.com   
 * @Date: 2011-7-2 下午03:33:39  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.network;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

import com.esun.config.Language;
import com.esun.model.UserInfo;

/**
 * @ClassName: ConnectionChangeReceiver
 * @Description: TODO(网络状态监测类)
 * @Author: Billy qigs@500wan.com
 * @Date: 2011-7-2 下午03:33:39
 */
public class ConnectionChangeReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		// NetworkInfo activeNetInfo =
		// connectivityManager.getActiveNetworkInfo();
		NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

		if ((mobNetInfo == null || !mobNetInfo.isConnected()) && (wifiNetInfo == null || !wifiNetInfo.isConnected())) {
			if (((Activity) context).getParent() != null) {
				//ViewerUtil.showTipDialog(((Activity) context).getParent(), Language.STR_CONNECTIONFAIL, false);
			  Toast.makeText(((Activity) context).getParent(), Language.STR_CONNECTIONFAIL, Toast.LENGTH_SHORT).show();
			} else {
				//ViewerUtil.showTipDialog(context, Language.STR_CONNECTIONFAIL, false);
			  Toast.makeText(context, Language.STR_CONNECTIONFAIL, Toast.LENGTH_SHORT).show();
			}
			UserInfo.getUserInfo().setNetWork(false);

		} else {
			UserInfo.getUserInfo().setNetWork(true);
		}
	}

	private static IntentFilter filter = null;

	public static IntentFilter getIntentFilter() {

		if (filter == null) {
			return filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
		} else {
			return filter;
		}
	}

	private static ConnectionChangeReceiver connectionChangeReceiver = null;

	public static ConnectionChangeReceiver getConnectionChangeReceiver() {

		if (connectionChangeReceiver == null) {
			return connectionChangeReceiver = new ConnectionChangeReceiver();
		} else {
			return connectionChangeReceiver;
		}
	}
}
